package com.wang.gulimall.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.common.utils.R;
import com.wang.gulimall.product.entity.CategoryEntity;
import com.wang.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class TestTreeContoller {

    @Autowired
    private CategoryService categoryService;

    public R testMybatisLambda(){
        R resMap = R.ok();
        LambdaQueryWrapper<CategoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryEntity::getName,"123");
        categoryService.list(queryWrapper);
        return resMap;
    }

    @RequestMapping("/tree")
    public R test() {
        R resMap = R.ok("成功");
        //数据库中差的全部的list
        List<CategoryEntity> list = categoryService.list();
//        resMap.put("原list",list);

        List<CategoryEntity> after = list.stream()
                .filter(l -> l.getParentCid() == 0)
                //这里这样写,需要使用Lombok的 @Accessors(chain = true)链式编程,执行set方法之后会将set之后的实体类返回
                //现在需要一个方法,传入全部的列表和当前实体类,返回当前实体类的孩子节点
                .map(l->l.setChildren(getTragetChildren(l,list)))
                .sorted((categoryEntity1, categoryEntity2) -> {
                    return (categoryEntity1.getSort() == null ? 0 : categoryEntity1.getSort()) - (categoryEntity2.getSort() == null ? 0 : categoryEntity2.getSort());
                })
                .collect(Collectors.toList());
        resMap.put("加工后的", after);

        return resMap;
    }

    /**
     *
     * @param targetCategoryEntity 目标商品三级分类实体类
     * @param all 所有的三级分类
     * @return 目标商品三级分类实体类的子类
     */
    public List<CategoryEntity> getTragetChildren(CategoryEntity targetCategoryEntity,List<CategoryEntity> all){
        List<CategoryEntity> resList = all.stream()
                //过滤器
                //all的每一个元素的pid == 目标的id的话，就说明这个元素是该目标的子类菜单
                .filter(res -> targetCategoryEntity.getCatId() == res.getParentCid())
                //对经过filter过滤的元素进行操作，再次执行该方法（递归）寻找子类的子类
                .map(l->l.setChildren(getTragetChildren(l, all)))
                //排序
                .sorted((categoryEntity1, categoryEntity2) -> {
                    return (categoryEntity1.getSort() == null ? 0 : categoryEntity1.getSort()) - (categoryEntity2.getSort() == null ? 0 : categoryEntity2.getSort());
                })
                .collect(Collectors.toList());
        return resList;
    }

    //测试mp的分页查询
    @RequestMapping("/test/catepage/{size}")
    public R cateBypage(@PathVariable("size") Long size){
        IPage<CategoryEntity> page = categoryService.page(new Page<CategoryEntity>(1, size), null);
        return R.ok().put("page",page);
    }
}
